package com.situ.shoplook.common.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.situ.shoplook.common.mapper.GoodsInfoMapper;
import com.situ.shoplook.common.mapper.OrderItemMapper;
import com.situ.shoplook.common.mapper.OrderMapper;
import com.situ.shoplook.common.mapper.UserMapper;
import com.situ.shoplook.common.model.GoodsInfo;
import com.situ.shoplook.common.model.Order;
import com.situ.shoplook.common.model.ShoppingCart;
import com.situ.shoplook.common.model.User;
import com.situ.shoplook.common.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author xinchen
 * @Date: 2024/09/03/ 10:20
 * @Description
 */
@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private OrderItemMapper orderItemMapper;

    @Autowired
    private GoodsInfoMapper goodsInfoMapper;


    /**
     * add
     */
    @Override
    public int save(Order order) throws Exception {

        return this.orderMapper.insertSelective(order);
    }

    /**
     * 更新
     * @throws Exception
     */
    @Override
    public int update(Order order) throws Exception {
        //判断订单号不能
        return orderMapper.updateByPrimaryKeySelective(order);
    }

    /**
     * 删除
     * @return
     */
    @Override
    public int remove(Integer[] ids) {
        int count =  orderMapper.deleteByIds(ids);
        return count;
    }

    /**
     * 分页
     * @return
     */
    @Override
    public PageInfo<Order> searchByPage(Integer page, Integer limit, Order order)  {
        List<Order> list1 = orderMapper.search(order);
        System.out.println(list1.toString());
        try(Page<?> __ = PageHelper.startPage(page, limit)) {
            List<Order> list = orderMapper.search(order);
            for (Order order1 : list) {
                order1.setUser(userMapper.selectByPrimaryKey(order1.getUserId()));
                order1.setOrderItem(orderItemMapper.selectByOrderId(order1.getOrderId()));
                System.out.println(order1.toString()+ "44444444444444");
            }
            PageInfo pageInfo = new PageInfo<>(list);
            return pageInfo;
        }
    }

    /**
     * 根据订单号查询
     * @param orderNo
     * @param userId
     * @return
     */
    @Override
    public Order selectByOrderNo(String orderNo, Long userId) {
        Order order = orderMapper.selectByPrimaryKey(Long.valueOf(orderNo));
        if(order == null){
            throw new RuntimeException("订单不存在");
        }
        //验证是否是当前userId下的订单，否则报错
        if(!order.getUserId().equals(userId)){
            throw new RuntimeException("非当前用户的订单");
        }
        /**
         * 封装订单详情
         */
        order.setOrderItem(orderItemMapper.selectByOrderId(order.getOrderId()));
        return order;
    }

    /**
     * 创建订单
     * @param user
     * @param myshopCart
     * @return 订单编号
     */
    @Override
    public String saveOrder(User user, List<ShoppingCart> myshopCart) {
        /**
         * 获取所有的购物车
         */
        List<Long> itemIdList = myshopCart.stream().map(ShoppingCart::getCartId).collect(Collectors.toList());
        List<Long> goodsIds = myshopCart.stream().map(ShoppingCart::getGoodsId).collect(Collectors.toList());
        List<GoodsInfo> goodsInfos = goodsInfoMapper.selectByPrimaryKeys(goodsIds);
        // 检查是否商品包含已下架商品
        for (ShoppingCart cartItem : myshopCart) {
            for (GoodsInfo goodsInfo : goodsInfos) {
                if (cartItem.getGoodsId().equals(goodsInfo.getGoodsId()) && !goodsInfo.getGoodsSellStatus().equals(1)) {
                    throw new RuntimeException("商品已下架，无法生成订单");
                }
            }
        }
        // 生成订单号
        //判断商品库存
        for (ShoppingCart cartItem : myshopCart) {
            for (GoodsInfo goodsInfo : goodsInfos) {
                if (cartItem.getGoodsId().equals(goodsInfo.getGoodsId()) && cartItem.getGoodsCount() > goodsInfo.getStockNum()) {
                    throw new RuntimeException("库存不足，无法生成订单");
                }
            }
        }
        // 删除购物车,生成订单
        return  "";
    }

}
